package main

import (
	"fmt"
	"log"
	"net"
)

func main() {
	//libgoffi_test() // ok
	//ret := cgo_ffi_std_dylib(2., 3.) //ok
	//ret := cgo_ffi_add_static(1, 2) //ok
	//cgo_ffi_add_dylibfoo(1, 2) //error
	cgo_ffi_strlen_static("abc") //ok

	subject := "a;jhgoqoghqoj0329 u0tyu10hg0h9Y0Y9827342482y(Y0y(G)_)lajf;lqjfgqhgpqjopjqa=)*(^!@#$%^&*())9999999"
	pattern := "\\d{4}([a-zA-Z_()]{3,11})\\S+"
	//results := cgo_preg_match(pattern, subject) // ok
	results := ffi_call_preg_match_static(pattern, subject) // ok
	//results := ffi_call_cgo_preg_match(pattern, subject) // ok
	//results := ffi_call_preg_match_dylibmypcre2(pattern, subject) // ok
	var result = results[1]

	// only one roundtrip , not repl
	ip := net.ParseIP("127.0.0.1")
	srcAddr := &net.UDPAddr{IP: net.IPv4zero, Port: 0}
	dstAddr := &net.UDPAddr{IP: ip, Port: 9981}
	conn, err := net.DialUDP("udp", srcAddr, dstAddr)
	if err != nil {
		log.Fatalln(err)
	}
	defer conn.Close()

	conn.Write([]byte(result))

	data := make([]byte, 1024)
	var n int
	if n, err = conn.Read(data); err != nil {
		log.Fatalln(err)
	}
	fmt.Println(string(data[:n]))
}

//go build -o main && ./main
// debug: dir选/Users/leon/github/mysock/cgo/myregexfun即可
